<?php

declare(strict_types=1);

namespace App\Controller\AdminApi;

use Dotenv\Validator;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\Utils\Arr;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use App\Exception\XZXValidationException;
use App\Service\CurrencyCodeService;
use App\Utils\CustomUtils;
use App\Utils\DateUtils;
use Psr\Container\ContainerInterface;
use Hyperf\Logger\LoggerFactory;
use Hyperf\Contract\TranslatorInterface;
use App\Constants\XZXResponseCode;

/**
 * Class CurrencyCodeController
 *
 * @package App\Controller
 * @Controller(prefix="/admin/api/currencyCode")
 */
class CurrencyCodeController extends AdminBaseController {
    /**
     * @Inject
     * @var TranslatorInterface
     */
    private $translator;

    /**
     * @Inject()
     * @var ValidatorFactoryInterface
     */
    protected $validationFactory;

    /**
     * @Inject()
     * @var CurrencyCodeService
     */
    protected $CurrencyCodeService;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    public function __construct (LoggerFactory $loggerFactory) {
        // 第一个参数对应日志的 name, 第二个参数对应 config/autoload/logger.php 内的 key
        $this->logger = $loggerFactory->get('log', 'default');
    }

    private function setLocale () {
        if ($this->request->header('locale') == 'en') {
            $this->translator->setLocale('en');
        }
    }

    /**
     * 查询
     * @PostMapping(path = "list")
     */
    public function list (RequestInterface $request) {
        $this->setlocale();

        $validator = $this->validationFactory->make($request->all(), []);

        if ($validator->fails()) {
            // Handle exception
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }

        // $param = $request->all();
        $param['isBoc'] = 1;

        // $search = $request->input("search", []);

        // CustomUtils::xzxAddSearch($param, $search, "isBoc");
        // CustomUtils::xzxAddSearch($param, $search, "code");
        // CustomUtils::xzxAddSearch($param, $search, "name");

        return $this->CurrencyCodeService->list($param);
    }

}
